package com.xnfh.home.utils

import android.app.Activity
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.xnfh.common.constant.KvKey
import com.xnfh.framework.log.LogUtil
import com.xnfh.framework.utils.KVUtil
import com.xnfh.home.imp.AMapLocationImp

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/13 10:02
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/13 10:02
 * @UpdateRemark: 更新说明
 */
object LocationUtil: AMapLocationListener {

    private var mLocationClient: AMapLocationClient? = null
    private var mLocationOption: AMapLocationClientOption? = null

    private var aMapLocationImp: AMapLocationImp? = null

    fun initLocation(activity: Activity,aMapLocation: AMapLocationImp) {
        this.aMapLocationImp = aMapLocation
        //初始化client
        mLocationClient = AMapLocationClient(activity)
        //设置定位参数
        mLocationClient?.setLocationOption(getDefaultOption())
        // 设置定位监听
        mLocationClient?.setLocationListener(this)
    }

    fun startLocation(){
        //启动定位
        mLocationClient?.startLocation()
    }

    private fun getDefaultOption(): AMapLocationClientOption? {
        mLocationOption = AMapLocationClientOption()
        //可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mLocationOption?.locationMode = AMapLocationClientOption.AMapLocationMode.Battery_Saving
        //可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mLocationOption?.isGpsFirst = true
        //可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mLocationOption?.httpTimeOut = 30000
        //可选，设置定位间隔。默认为2秒
        mLocationOption?.interval = 1000
        //可选，设置是否返回逆地理地址信息。默认是true
        mLocationOption?.isNeedAddress = true
        //可选，设置是否单次定位。默认是false
        mLocationOption?.isOnceLocation = true
        //可选，设置是否使用缓存定位，默认为true
        mLocationOption?.isLocationCacheEnable = true
        //可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        mLocationOption?.isOnceLocationLatest = false
        //可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP)
        //可选，设置是否使用传感器。默认是false
        mLocationOption?.isSensorEnable = true
        //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mLocationOption?.isWifiScan = true
        return mLocationOption
    }

    override fun onLocationChanged(aMapLocation: AMapLocation?) {
        if (aMapLocation != null && aMapLocation.errorCode == 0) {
            LogUtil.i("onLocationChanged: " + aMapLocation.province + "---" + aMapLocation.city + "---" + aMapLocation.district + "---" + aMapLocation.latitude + "---" + aMapLocation.longitude)
            val latitude: Double = aMapLocation.latitude
            val longitude: Double = aMapLocation.longitude
            KVUtil.put(KvKey.LOCATION_INFO, "$longitude,$latitude")
            aMapLocationImp?.onLocationChanged()
            mLocationClient!!.stopLocation()
        }else {
            aMapLocationImp?.onLocationError()
        }
    }
}